/*DxfBLOCK.java
 *
 * Created on 6 de Dezembro de 2007, 10:24
 *
 * Modified 08/10/2011, 02:30 PM by Adriano
 * Included the attribute @Override to inherited methods
 */


package importadorDXF;

import java.util.Enumeration;
import mathematics.Point3D;

/**
 * @author rafaelbuck
 * @author Adriano Ferreira
 */
public class DxfBLOCK extends DxfEntity implements DxfEntityCollector
{
    /** Creates a new instance of DxfBLOCK */
    public DxfBLOCK()
    {
        refPoint = new Point3D();
        entitySet = new DxfEntitySet();
        super.color = 0;
    }

    @Override
    public boolean setGroup(short grpNr, String str)
    {
        switch(grpNr)
        {
        case 1: // '\001'
            external = str;
            return true;

        case 2: // '\002'
            blockName = str;
            return true;

        case 3: // '\003'
            typeName = str;
            return true;
        }
        return super.setGroup(grpNr, str);
    }

    @Override
    public boolean setGroup(short grpNr, float fval)
    {
        switch(grpNr)
        {
        case 10: // '\n'
        case 20: // '\024'
        case 30: // '\036'
            setCoord(refPoint, grpNr / 10, fval);
            return true;
        }
        return super.setGroup(grpNr, fval);
    }

    @Override
    public boolean setGroup(short grpNr, int ival)
    {
        if(grpNr == 70)
        {
            type = ival;
            return true;
        } else
        {
            return super.setGroup(grpNr, ival);
        }
    }

    @Override
    public boolean isBlockNamed(String name)
    {
        return blockName != null && blockName.equals(name);
    }

    @Override
    public boolean addEntity(DxfEntity entity) throws DxfException
    {
        if(entity instanceof DxfBLOCK)
            throw new DxfException("err!Nested");
        if(entity instanceof DxfENDBLK)
            return false;
        if(entity instanceof DxfATTDEF)
        {
            if(attribSet == null)
                attribSet = new DxfEntitySet();
            attribSet.addEntity(entity);
        } else
        {
            entitySet.addEntity(entity);
        }
        return true;
    }

    @Override
    public DxfEntityCollector getEntityCollector()
    {
        return this;
    }

    @Override
    public void convert(DxfConverter converter, DxfFile dxf, Object collector)
    {
        converter.convert(this, dxf, collector);
    }

    public final String getBlockName()
    {
        return blockName;
    }

    public final String getTypeName()
    {
        return typeName;
    }

    public final String getExternal()
    {
        return external;
    }

    public final int getType()
    {
        return type;
    }

    public final Point3D getReferencePoint()
    {
        return refPoint;
    }

    public final DxfEntitySet getEntities()
    {
        return entitySet;
    }

    public DxfEntitySet getAttdefSet()
    {
        return attribSet;
    }

    @Override
    public short getColor()
    {
        return 0;
    }

    @Override
    public String toString()
    {
        return "BLOCK " + blockName;
    }

    @Override
    public boolean finishRead(DxfFile file)
    {
        if(refPoint.z != 0.0F)
        {
            for(Enumeration e = entitySet.getEntities(); e.hasMoreElements(); ((DxfEntity)e.nextElement()).setExternalZ(refPoint.z));
        }
        return super.finishRead(file);
    }

    protected String blockName;
    protected String typeName;
    protected String external;
    protected int type;
    protected Point3D refPoint;
    protected DxfEntitySet entitySet;
    protected DxfEntitySet attribSet;
}